// store/index.ts
import { createStore } from "vuex";

interface State {
  isLoggedIn: boolean;
  isCollapsed: boolean;
  darkTheme: boolean;
  isLocked: boolean;
  currentRole: {
    id: number;
    role_name: string;
    role_avatar: string;
    role_age: string;
    role_sex: string;
    role_description: string;
  },
  currentSession: {
    role_id: number;
    role_name: string;
    chat_record_id: number;
    chat_title: string;
  };
  userInfo: {
    id: number;
    avatar: string;
    account: string;
    nickname: string;
  };
  token: string;
}

export default createStore({
  state: <State>{
    isLoggedIn: false,
    isCollapsed: false,
    darkTheme: false,
    isLocked: false,
    currentRole: {
      id: 0,
      role_name: "",
      role_avatar: "",
      role_age: "",
      role_sex: "",
      role_description: "",
    },
    currentSession: {
      role_id: 0,
      role_name: "",
      chat_record_id: 0,
      chat_title: "",
    },
    userInfo: {
      id: 0,
      avatar: "",
      account: "",
      nickname: "",
    },
    token: "",
  },
  mutations: {
    setLoginStatus(state, status) {
      state.isLoggedIn = status;
      localStorage.setItem("isLoggedIn", JSON.stringify(status));
    },
    setCollapseStatus(state, status) {
      state.isCollapsed = status;
      localStorage.setItem("isCollapsed", JSON.stringify(status));
    },
    setCurrentRole(state, role) {
      state.currentRole = role;
      localStorage.setItem("currentRole", JSON.stringify(role));
    },
    setCurrentSession(state, session) {
      state.currentSession = session;
      localStorage.setItem("currentSession", JSON.stringify(session));
    },
    setDarkTheme(state, theme) {
      state.darkTheme = theme;
      localStorage.setItem("darkTheme", JSON.stringify(theme));
    },
    setLockStatus(state, status) {
      state.isLocked = status;
      localStorage.setItem("isLocked", JSON.stringify(status));
    },
    setUserInfo(state, user) {
      state.userInfo = user;
      localStorage.setItem("userInfo", JSON.stringify(user));
    },
    setToken(state, token) {
      state.token = token;
      localStorage.setItem("token", token);
    },
    clearCurrentUser(state) {
      state.userInfo = {
        id: 0,
        avatar: "",
        account: "",
        nickname: "",
      };
      localStorage.removeItem("userInfo");
    },
    clearCurrentSession(state) {
      state.currentSession = {
        role_id: 0,
        role_name: "",
        chat_record_id: 0,
        chat_title: "",
      };
      localStorage.removeItem("currentSession");
    },
    clearToken(state) {
      state.token = "";
      localStorage.removeItem("token");
    },
  },
  actions: {
    login({ commit }) {
      commit("setLoginStatus", true);
    },
    logout({ commit }) {
      commit("setLoginStatus", false);
      commit("clearCurrentUser");
      commit("clearToken");
    },
  },
  getters: {
    isLoggedIn: (state) => state.isLoggedIn,
    isCollapsed: (state) => state.isCollapsed,
    isLocked: (state) => state.isLocked,
    currentSession: (state) => state.currentSession,
    darkTheme: (state) => state.darkTheme,
    userInfo: (state) => state.userInfo,
    token: (state) => state.token,
  },
});
